<?php
class Banners extends Controller {

    function Banners()
    {
        parent::Controller();
        $this->load->helper(array('form','tiap_helper','html'));
        $this->load->library('form_validation','url');
        $this->load->model(array('User_model','Banners_model','Directory_user_model'));
        $this->load->library('session');
		 if($this->session->userdata('admin') == '')
		 {
			 redirect('admin');
		 }

    }

    function processing($ban_id)
    {
         $inserted = $this->Banners_model->insertBannersClick($ban_id);
	 $ban_link=$this->Banners_model->getBannersField($ban_id,'ban_link');

            if($ban_link == '')
        	$ban_link = site_url();
	    header("Location:".prep_url($ban_link));
            exit();



        //print_r($data);
       // echo $ban_id;
       // die();

    }
    
    function index()
    {    
       //  $this->load->view(TEMPLATE_NAME.'/admin/banners/category');
         $this->load->view(TEMPLATE_NAME.'/admin/banners/category',$data);

    }

    function active()
    {
        $data['ban_category'] = 'Active Banners';
        $data['ban_detail']=$this->Banners_model->activeBanners();
         $this->load->view(TEMPLATE_NAME.'/admin/banners/list',$data);
   }

    function inactive()
    {
        $data['ban_category'] = 'Inactive Banners';
        $data['ban_detail']=$this->Banners_model->inactiveBanners();
         $this->load->view(TEMPLATE_NAME.'/admin/banners/list',$data);
    }
     function defaultBan()
    {
        $data['ban_category'] = 'Default Banners';
        $data['ban_detail']=$this->Banners_model->defaultBanners();
         $this->load->view(TEMPLATE_NAME.'/admin/banners/list_def',$data);

    }
    function payment_select(){
        $package_id=$this->uri->segment(3);
    if(!$package_id)
        redirect('register/packages');
    else
   $this->session->set_userdata('packages',$package_id);
   $this->load->view(TEMPLATE_NAME."/admin/banners/make_a_payment_view",$data);
    }
   function payment_type(){
    
        if(isset($_POST['submit_payment_x']) || isset($_POST['submit_payment']))
		{
                         
       $paymentType = $this->input->post('payment_type');
       $this->session->set_userdata('payment_type',$paymentType);
        $user_detail = $this->session->userdata('user_info');
                    
                   $name = $user_detail['name'];
                     $company = $user_detail['company'];
                    $telephone = $user_detail['telephone'];
                    $email = $user_detail['email'];
                    $this->load->model(array('Register_model'));
                    $packages_detail = $this->Register_model->get_package_offers_byId($this->session->userdata('packages'));
                 
                    $payfor = $packages_detail['package_type'];
                    $country = 'US';
                    $amount = $packages_detail['cost'];
                    $arr = array('name' => $name,'companyName' => 'customer','telephone' =>$telephone,'email' =>$email,'payfor' =>$payfor,'country' => $country, 'amount' => $amount, 'paymentType' => $paymentType);
                       
                if($paymentType == 1)
                {
                    $this->load->view(TEMPLATE_NAME."/admin/banners/make_a_payment_creditcard_view",$arr);

                }
                else
                { $this->load->model(array('make_a_payment_model'));
                    $data['bankdetails'] = $this->make_a_payment_model->getBAnkDetails();
                    
                    $orderid = $this->make_a_payment_model->insertpayment($arr);
                   // $this->session->set_userdata('register_message', "");

                    
                    //$directory_user_id = $this->registration_process();
                   // if($directory_user_id)
                       $data['payment_confirm'] = 'Thank you for placing your ads on Kyprisguide.com. Please print this invoice and mail it along with your payment (link to printable version of the invoice)';

                    $this->make_a_payment_model->sendemailBybanktype($orderid);
                     $data = array_merge($data,$arr);    
                    $this->load->view(TEMPLATE_NAME."/admin/banners/make_a_payment_banktransfer_view",$data);

                }
			}
			else
			{
				//unset($data);
                //$data['recaptcha']=$this->recaptcha->get_html();


            $this->load->view(TEMPLATE_NAME."/admin/banners/make_a_payment_view",$data);
           // redirect('register/make_a_payment_view');
        }



}
function payment()
    { 
      
    
        $this->load->model('make_a_payment_model');

        if(isset($_POST['submit_payment_x']) || isset($_POST['submit_payment']))
		{
                
                $this->_confirmpayemnt();
                }else
                    {
				//unset($data);
                //$data['recaptcha']=$this->recaptcha->get_html();


            $this->load->view(TEMPLATE_NAME."/admin/banners/make_a_payment_view",$data);
           // redirect('register/make_a_payment_view');
            }
    }

    function _confirmpayemnt()
    { //redirect(register/categories);
        $this->load->model('make_a_payment_model');
         $user_detail = $this->session->userdata('user_info');
         $paymentType = $this->session->userdata('payment_type');
                $name = $user_detail['name'];
                  // $company = $user_detail['company'];
                    $telephone = $user_detail['telephone'];
                    $email = $user_detail['email'];
                    $this->load->model(array('Register_model'));
                    $packages_detail = $this->Register_model->get_package_offers_byId($this->session->userdata('packages'));
                 
                    $payfor = $packages_detail['package_type'];
                    $country = 'US';
                    $amount = $packages_detail['cost'];
                    $arr = array('name' => $name,'companyName' => 'customer','telephone' =>$telephone,'email' =>$email,'payfor' =>$payfor,'country' => $country, 'amount' => $amount, 'paymentType' => $paymentType);
                       // $data = array_merge($data,$arr);
        

        $orderid = $this->make_a_payment_model->insertpayment($arr);
        // $this->session->set_userdata('register_message',"Thank you for placing your ads on Kyprisguide.com. You can manage your ads at (link to advertiser management panel)");
 
        redirect('banners/action');
        
}


     function action()
    {   $ban_id=$this->uri->segment(4);
             $submit_button=$this->input->post('ban_action_submit');
                if($submit_button != ""){ 
                $this->load->library('form_validation');
               	$this->form_validation->set_error_delimiters('<div class="error" >','</div>');
               //$this->form_validation->set_rules('text_detail', 'ban detail','required');
               $this->form_validation->set_rules('ban_name', ' name','required');
                $this->form_validation->set_rules('ban_impression', ' impression','required');

                if($this->form_validation->run() == TRUE){
                    $ban_status=$this->input->post('ban_status');
                   $ban_name= trim($this->input->post('ban_name'));
                   $ban_date_from=$this->input->post('ban_date_from');
                   $ban_placement=$this->input->post('ban_placement');
                   $ban_date_from_time_hour=$this->input->post('ban_date_from_time_hour');
                   $ban_date_from_time_min=$this->input->post('ban_date_from_time_min');
                   $ban_date_to=$this->input->post('ban_date_to');
                   $ban_date_to_time_hour=$this->input->post('ban_date_to_time_hour');
                   $ban_date_to_time_min=$this->input->post('ban_date_to_time_min');
                   $ban_impression=$this->input->post('ban_impression');
                    $ban_link=$this->input->post('ban_link');
                    $ban_tag=$this->input->post('ban_tag');
                   $ban_status=$this->input->post('ban_status');
                   /* Rename a upload image name to banner title name */
                        $ban_upload_filename=$this->input->post('upload');
                        if($ban_upload_filename!='')
                       {
                       $ban_logo = $this->logo_rename($ban_upload_filename,$ban_name);}
                            else{
                            $ban_logo='default.jpg';
                            }

                       if($ban_date_from != '')
                       {
                        $ban_date_from = $ban_date_from.' '.$ban_date_from_time_hour.':'.$ban_date_from_time_min;
                       }
                       if($ban_date_to != '')
                       {
                       $ban_date_to = $ban_date_to.' '.$ban_date_to_time_hour.':'.$ban_date_to_time_min;
                       }
                      /* Adding data to array*/
                   $ban_data=array(
                   'ban_status' => $ban_status,
                    'ban_image' => $ban_logo,
                    'ban_name' => $ban_name,
                    'ban_placement' => $ban_placement,

                    'ban_impression' => $ban_impression,
                    'ban_link' => $ban_link,
                    'ban_tag' => $ban_tag
                                        );
                  $ban_edit_id=$this->input->post('ban_id');
                   if($ban_edit_id==''){
                    $ban_data['ban_posted_date']= date("Y-m-d H:i:s");
                    $ban_data['ban_date_from'] = date("Y-m-d H:i:s");
                    $insert_ban=$this->Banners_model->insertBannersData($ban_data);
                       }
                       else{ //echo $ban_edit_id;die();

                      // $this->db->select('ban_status')->from('tbl_banners')->where('ban_id',$ban_edit_id);
                      // $this->db->where('ban_id',$ban_id);
                      //$query = $this->db->get();
                      //$data = $query->row_array();

                     // if($data['ban_status'] == '0' && $ban_status == '1')
                     // {
                    // $ban_data['ban_date_from'] = date("Y-m-d H:i:s");
                    // $ban_data['ban_date_to'] = '0';
		     //$ban_data['ban_track_view'] = '0';
		    // $ban_data['ban_track_click'] = '0';
             $ban_data=array(
                   'ban_status' => $ban_status,
                    'ban_image' => $ban_logo,
                    'ban_name' => $ban_name,
                    'ban_placement' => $ban_placement,

                    'ban_impression' => $ban_impression,
                    'ban_link' => $ban_link,
                    'ban_tag' => $ban_tag
                                        );
                       // }
                         $edit_ban=$this->Banners_model->updateBannersData($ban_edit_id,$ban_data);

                       }
                     redirect('admin/banners');
                    }
           // $this->db->last_query();
           //die();
              }
           if($ban_id){
		$ban_detail = $this->Banners_model->detailBanners($ban_id);
      	$data['ban_detail'] = $ban_detail;
            }
		    $this->load->view(TEMPLATE_NAME.'/admin/banners/action',$data);
    }
    
    function success(){
       $this->load->view(TEMPLATE_NAME.'/admin/banners/success',$data); 
    }
     function action_def()
    {   $ban_id=$this->uri->segment(4);
             $submit_button=$this->input->post('ban_action_submit');
                if($submit_button != ""){

                /* Rename a upload image name to banner title name */
                        $ban_upload_filename=$this->input->post('upload');
                        $ban_link=$this->input->post('ban_link');
                    $ban_tag=$this->input->post('ban_tag');

                     $ban_logo = $this->logo_rename($ban_upload_filename,'default');
                      /* Adding data to array*/
                   $ban_data=array(
                'ban_image' =>$ban_logo,
                'ban_link' => $ban_link,
                'ban_tag' => $ban_tag
                                     );
                  $ban_edit_id=$this->input->post('ban_id');
                   if($ban_edit_id != ''){

                         $edit_ban=$this->Banners_model->updateBannersDefault($ban_edit_id,$ban_data);

                       }
                     redirect('admin/banners/defaultBan');

           //echo  $this->db->last_query();
           //die();
              }
           if($ban_id){
		$ban_detail = $this->Banners_model->getDefaultBanners($ban_id);
      	$data['ban_detail'] = $ban_detail;

            }
		    $this->load->view(TEMPLATE_NAME.'/admin/banners/action_def',$data);
    }
    function delete(){

     $ban_id=$this->uri->segment(4);
     if($ban_id!=''){
        $ban_detail=$this->Banners_model->getBanners($ban_id);
        $ban_image=$ban_detail['ban_image'];

        if($ban_image!='')
       {
        $this->logo_delete($ban_image);
        }
       $this->Banners_model->deleteBanners($ban_id);
        redirect('admin/banners');
       }


    }
     function logo_rename($oldname,$newname){
                    $ext_arr=explode('.',$oldname);
                    $ext=$ext_arr['1'];
                    $newname=str_replace(' ','_',$newname);
                    $newname=$newname.rand().'.'.$ext;
        if(file_exists('./uploads/banners/'.$oldname) && file_exists('./uploads/banners/thumb/'.$oldname))
                    {
                    @rename('./uploads/banners/'.$oldname,'./uploads/banners/'.$newname);
                    @rename('./uploads/banners/thumb/'.$oldname,'./uploads/banners/thumb/'.$newname);
                     }
                      return $newname;
    }

     function logo_delete($filename=''){
        // $filename = $this->uri->segment(3);
        if($filename=='')
            $filename=$this->input->post('filename');

        if( file_exists('./uploads/banners/'.$filename))
               @unlink('./uploads/banners/'.$filename);

        if( file_exists('./uploads/banners/thumb/'.$filename))
                 @unlink('./uploads/banners/thumb/'.$filename);


        }

}